﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodePlex.KeyedList
{
	/// <summary>
	/// An generic EqualityComparer for objects of the Key class
	/// </summary>
	/// <typeparam name="T1"></typeparam>
	/// <typeparam name="T2"></typeparam>
	public class KeyEqualityComparer<T1, T2> : EqualityComparer<Key<T1, T2>>
	{

		static KeyEqualityComparer()
		{
			_comparer1 = EqualityComparer<T1>.Default;
			_comparer2 = EqualityComparer<T2>.Default;
		}

		private static IEqualityComparer<T1> _comparer1;
		private static IEqualityComparer<T2> _comparer2;

		/// <summary>
		/// Returns the hash code for passed in Key
		/// </summary>
		/// <param name="obj">obj to generate hashcode for</param>
		/// <returns></returns>
		public override int GetHashCode(Key<T1, T2> obj)
		{
			return _comparer1.GetHashCode(obj.Value1) ^ _comparer2.GetHashCode(obj.Value2);
		}

		/// <summary>
		/// Indicates whether two Keys are equal.
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		public override bool Equals(Key<T1, T2> x, Key<T1, T2> y)
		{
			return _comparer1.Equals(x.Value1, y.Value1)
				&& _comparer2.Equals(x.Value2, y.Value2);
		}

	}
}
